//
//  ShuwuDetailViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "ShuwuDetailViewController.h"
#import "ShuwuModel.h"

@interface ShuwuDetailViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) UILabel *label;

@end

@implementation ShuwuDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //导航栏标题
    self.navigationItem.title = self.shuwuModel.short_title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    //请求数据
    NSString *string = self.shuwuModel.content_url;
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.scrollView.delegate = self;
    self.webView.scrollView.userInteractionEnabled = YES;
    [self.view addSubview:self.webView];
    
    
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(200 * WIDTHSCALE, 0, 0, 0);
    //相对于0点,已经向下偏移了-200
    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, -200 * HEIGHTSCALE, WIDTH, 200 * HEIGHTSCALE)];
    [self.imageview sd_setImageWithURL:[NSURL URLWithString:self.shuwuModel.cover_image_url]];
    self.imageview.contentMode = UIViewContentModeScaleAspectFill;//设置imageView高度改变时宽度也跟着改变
    [self.webView.scrollView addSubview:self.imageview];

    //标题
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, -50 * HEIGHTSCALE, WIDTH - 10, 50 * HEIGHTSCALE)];
    self.label.text = self.shuwuModel.title;
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:17];
    [self.webView.scrollView addSubview:self.label];
    
//    //webview有黑块的问题和到边框下拉或上拉有灰块
//    for (UIScrollView* view in self.webView.subviews)
//    {
//        if ([view isKindOfClass:[UIScrollView class]])
//        {
//            view.bounces = NO;
//        }
//    }
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.webView setOpaque:NO];//设置控件是否透明
    
}

//下拉放大
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.webView.scrollView) {
        
        CGFloat y = scrollView.contentOffset.y;//加上导航栏高度+64,第一次是- 200
        if (y < -200 * HEIGHTSCALE) {
            CGRect frame = self.imageview.frame;
            frame.origin.y = y;//imageView的frame是不断往上偏移
            frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
            self.imageview.frame = frame;
        }
    }
}


//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    //判断是否是单击
//    if (navigationType == UIWebViewNavigationTypeLinkClicked)
//    {
//        NSURL *url = [request URL];
//        if([[UIApplication sharedApplication] canOpenURL:url])
//        {
//            [[UIApplication sharedApplication] openURL:url];
//        }
//        return NO;
//    }
//    return YES;
//}




//-(void)handleSingleTap:(UITapGestureRecognizer *)sender
//{
//    
//    CGPoint pt = [sender locationInView:self.webView];
//    
//    NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
//    
//    NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
//    
//    NSString * JsToGetHTMLSource = @"top.location.href";
//    
//    NSString * pageSource = [self.webView stringByEvaluatingJavaScriptFromString:JsToGetHTMLSource];
//    
//    NSLog(@"pageSource = %@",pageSource);
//    
//}




- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
